package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.beans.base.image;
import com.hdax.dm.beans.item.Item;
import com.hdax.dm.beans.item.cinema;
import com.hdax.dm.dto.qinziDto.*;
import com.hdax.dm.dto.seniorityDto;
import com.hdax.dm.feign.imagefeign;
import com.hdax.dm.mappers.multipleMapper;
import com.hdax.dm.mapstucts.qinziDtoMapper.*;
import com.hdax.dm.service.multipleService;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class multipleServiceImpl extends ServiceImpl<multipleMapper, Item> implements multipleService {

    @Autowired
    private getAdimgUrlDtoMapper getAdimgUrlDtoMapper;

    @Autowired
    private getCarouselDataDtoMapper getCarouselDataDtoMapper;

    @Autowired
    private com.hdax.dm.mapstucts.qinziDtoMapper.getGuessYouLikeDtoMapper getGuessYouLikeDtoMapper;

    @Autowired
    private com.hdax.dm.mapstucts.qinziDtoMapper.getWonderfulDataDtoMapper getWonderfulDataDtoMapper;

    @Autowired
    private com.hdax.dm.mapstucts.qinziDtoMapper.getPopularSearchDtoMapper getPopularSearchDtoMapper;

    @Autowired
    private com.hdax.dm.mapstucts.qinziDtoMapper.getCalendarDataDtoMapper getCalendarDataDtoMapper;

    @Autowired
    private com.hdax.dm.mapstucts.qinziDtoMapper.getParentChildDtoMapper getParentChildDtoMapper;

    @Autowired
    private imagefeign imagefeign;

    @Autowired
    private cinemaServiceImpl cinemaService;

//    获取 亲子首页 页面广告地址及信息页面
    @Override
    public CommonResponse<List<getAdimgUrlDto>> getAdimgUrl(Map<String,Object> params) {
        Integer itemTypeId = Integer.parseInt((String)params.get("itemTypeId")) ;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("itemType1Id",itemTypeId);
        queryWrapper.last("limit 5");
        List<Item> lists= baseMapper.selectList(queryWrapper);
        List<getAdimgUrlDto> dtos= getAdimgUrlDtoMapper.toTargetList(lists);
        dtos.forEach(dto->{
            image img= imagefeign.image(dto.getId(),0L,1L);
            String imgUrl= img == null ? "":img.getImgUrl();
            dto.setImgUrl(imgUrl);
        });
        return ResponseUtil.returnSuccess(dtos);
    }

//    获取亲子首页轮播图数据信息
    @Override
    public CommonResponse<List<getCarouselDataDto>> getCarouselData(Map<String, Object> params) {
        Integer itemTypeId = Integer.parseInt((String)params.get("itemTypeId")) ;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("itemType1Id",itemTypeId);
        queryWrapper.last("limit 4");
        List<Item> lists = baseMapper.selectList(queryWrapper);
        List<getCarouselDataDto> dtos = getCarouselDataDtoMapper.toTargetList(lists);
        dtos.forEach(dto ->{
            image image = imagefeign.image(dto.getId(),1L,1L);
            String imgUrl= image == null ? "":image.getImgUrl();
            dto.setImgUrl(imgUrl);
        });
        return ResponseUtil.returnSuccess(dtos);
    }

//    获取猜你喜欢界面数据信息
    @Override
    public CommonResponse<List<getGuessYouLikeDto>> getGuessYouLike(Map<String, Object> params) {
        Integer itemTypeId = Integer.parseInt((String)params.get("itemTypeId")) ;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("itemType1Id",itemTypeId);
        queryWrapper.last("limit 6");
        List<Item> lists = baseMapper.selectList(queryWrapper);
        List<getGuessYouLikeDto> dtos = getGuessYouLikeDtoMapper.toTargetList(lists);
        dtos.forEach(dto ->{
            image image = imagefeign.image(dto.getId(),2L,1L);
            String imgUrl= image == null ? "":image.getImgUrl();
            dto.setImgUrl(imgUrl);
        });
        return ResponseUtil.returnSuccess(dtos);
    }
//    获取精彩聚焦界面数据信息
    @Override
    public CommonResponse<List<getWonderfulDataDto>> getWonderfulData(Map<String, Object> params) {
        Integer itemTypeId = Integer.parseInt((String)params.get("itemTypeId")) ;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("itemType1Id",itemTypeId);
        queryWrapper.last("limit 8");
        List<Item> lists = baseMapper.selectList(queryWrapper);
        List<getWonderfulDataDto> dtos = getWonderfulDataDtoMapper.toTargetList(lists);
        dtos.forEach(dto ->{
            image image = imagefeign.image(dto.getId(),2L,1L);
            String imgUrl= image == null ? "":image.getImgUrl();
            dto.setImgUrl(imgUrl);
        });
        return ResponseUtil.returnSuccess(dtos);
    }
//    获取热门排行数据信息
    @Override
    public CommonResponse<List<getPopularSearchDto>> getPopularSearch(Map<String, Object> params) {
        Integer itemTypeId = Integer.parseInt((String)params.get("itemTypeId")) ;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("itemType1Id",itemTypeId);
        queryWrapper.last("limit 7");
        List<Item> lists = baseMapper.selectList(queryWrapper);
        List<getPopularSearchDto> dtos = getPopularSearchDtoMapper.toTargetList(lists);
        dtos.forEach(dto ->{
            image image = imagefeign.image(dto.getId(),2L,1L);
            String imgUrl= image == null ? "":image.getImgUrl();
            dto.setImgUrl(imgUrl);
        });
        return ResponseUtil.returnSuccess(dtos);
    }
//      获取日历栏界面数据信息
    @Override
    public CommonResponse<List<getCalendarDataDto>> getCalendarData(Map<String, Object> params) {
        Integer itemTypeId = Integer.parseInt((String)params.get("itemTypeId")) ;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("itemType1Id",itemTypeId);
        queryWrapper.eq("isRecommend",1);
        queryWrapper.last("limit 7");
        List<Item> lists = baseMapper.selectList(queryWrapper);

        List<getParentDto> dtos = getCalendarDataDtoMapper.toItemList(lists);
        List<getCalendarDataDto> getDto = new ArrayList<>();

        dtos.forEach(dto ->{
            /*远程调用*/
            image image = imagefeign.image(dto.getId(),2L,1L);
            String imgUrl= image == null ? "":image.getImgUrl();
            dto.setImgUrl(imgUrl);

            /*新建一个getCalendarDataDto*/
            getCalendarDataDto getCalendarDataDto = new getCalendarDataDto();
            /*给Dto的day传值*/
            LocalDate date= LocalDate.from(dto.getStartDate());
            String month=date.getMonth().getValue()+"";
            String day=date.getDayOfMonth()+"";
            if(date.getMonthValue()<10){
                month="0"+month;
            }
            if (date.getDayOfMonth()<10){
                day="0"+day;
            }

            getCalendarDataDto.setDay(Long.parseLong(date.getYear()+""+month+""+day));
            /*在新建一个内部集合*/
            List<getParentDto> list = new ArrayList<>();
            list.add(dto);
            /*把新建的集合传给Dto的集合*/
            getCalendarDataDto.setItemList(list);

            /*最后赋值给Dto*/
            getDto.add(getCalendarDataDto);
        });
        return ResponseUtil.returnSuccess(getDto);
    }

    @Override
    public CommonResponse<List<getParentChildDto>> getParentChild(Map<String, Integer> params) {
        Integer ageGroup = params.get("ageGroup");
        Integer limit = params.get("limit");

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("ageGroup",ageGroup);
        queryWrapper.last("limit "+limit);
        List<Item> lists = baseMapper.selectList(queryWrapper);

        Integer cinemaId = null;
        for (Item o:lists) {
            cinemaId=o.getCinemaId().intValue();
        }

        List<getParentChildDto> dtos = getParentChildDtoMapper.toTargetList(lists);

        for (getParentChildDto dto : dtos) {
            cinema c = cinemaService.selectOne(cinemaId);
            dto.setAreaId(c.getAreaId().toString());
            dto.setAreaName(c.getAreaName());
            dto.setAddress(c.getAddress());
        }

        return ResponseUtil.returnSuccess(dtos);
    }

}
